import { isFunction, isString } from 'lodash-es';
import { ComputedRef, reactive, Ref, onMounted, computed } from 'vue'
import { VxeGridProps } from 'vxe-table';
import { http } from "/@/utils/http";

export function useTableData(getProps: ComputedRef<YTableProps>, vxtGridProps: Ref<VxeGridProps>) {

  const isRemoteData = computed(() => !!getProps.value.remoteConfig)


  const remoteLoadData = (params: any) => {

    if (params['filters'] && Array.isArray(params['filters'])) {
      params['filters'] = JSON.stringify(params['filters'])
    }

    const { api, filterApi } = getProps.value.remoteConfig

    const finalApi = () => {
      if (isFunction(api)) {
        return api.call(this, params)
      } else if (isString(api)) {
        return http.get(api, { params })
      }
    }
    return finalApi() as Promise<PureHttpResult<YTableRemoteResult>>
  }
  return {
    isRemoteData,
    remoteLoadData
  }
}

